<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sites extends API_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('sites_model');
    }

    /**
    * 设置网站信息
    * @param name, introduction, logo
    */
    public function site_register()
    {
    	if($_POST) {
	        $this->form_validation->set_rules('name', '网站名称', 'required|trim');
	        $this->form_validation->set_rules('introduction', '网站介绍', 'required|trim');
	
	        if ($this->form_validation->run() == FALSE) 
	        {
	            //提示错误
	            error_output(validation_error_one());
	        } 
	        else 
	        {
	            $name = $this->input->post('name', TRUE);
	            $introduction = $this->input->post('introduction', TRUE);
	            
	            // 验证网站名称是否存在
	            if ($this->sites_model->get_site_by_name($name)) {
	                error_output('网站名称已经存在');
	            }
	            // 验证通过
	            $site_data = array(
	                'name' => $name,
	                'introduction' => $introduction,
	                'create_time' => time(),
	            );
	            //判断是否有上传logo
	            if (isset($_FILES['logo']['tmp_name']) && is_uploaded_file($_FILES['logo']['tmp_name'])) {
	                //上传
	                
	                $logo_data = file_upload('site_logo', 'logo');
	                if ($logo_data) {
	                    $site_data['logo_path'] = $logo_data['path'];
	                } else {
	                    error_output(validation_error_one('Logo upload fail'));
	                }
	            }
	            $site_insert_id = $this->sites_model->insert($site_data);
	            /*
	            $result = array(
	                'status' => 1,
	                'site_id' => $site_insert_id,
	            );
	            json_output($result);
	            */
	            header("Location:".base_url()."index.php/templates/get_template_list?site_id=".$site_insert_id);
	        }
    	}else{
    		$this->load->view("templates/basic.php");
    	}
    }

    

}
